package cn.car.controller;

import cn.car.biz.SysRoleBiz;
import cn.car.domains.vo.BasePage;
import cn.car.domains.vo.Result;
import cn.car.domains.vo.sysRole.AddPermissionReq;
import cn.car.domains.vo.sysRole.CreateRoleReq;
import cn.car.domains.vo.sysRole.RoleListReq;
import cn.car.domains.vo.sysRole.SysRoleRes;
import cn.car.domains.vo.sysRole.UpdateRoleReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/role")
@Tag(name = "sysRoleController", description = "角色管理相关接口")
public class SysRoleController {

    @Resource
    private SysRoleBiz sysRoleBiz;

    /**
     * 创建角色
     */
    @PostMapping("/create")
    @Operation(description = "创建角色接口：根据请求参数创建一个新的角色")
    public Result<Void> createRole(@RequestBody @Valid CreateRoleReq req) {
        sysRoleBiz.createRole(req);
        return Result.success();
    }

    /**
     * 根据ID获取角色信息
     */
    @GetMapping("/getById")
    @Operation(description = "根据角色ID查询角色信息，返回角色详情")
    public Result<SysRoleRes> getRoleById(@Parameter(schema = @Schema(name = "id", description = "角色id")) @RequestParam Long id) {
        return Result.success(sysRoleBiz.getRoleById(id));
    }

    /**
     * 根据角色编码获取角色信息
     */
    @GetMapping("/getByCode")
    @Operation(description = "根据角色编码查询角色信息，返回角色详情")
    public Result<SysRoleRes> getRoleByCode(@Parameter(schema = @Schema(name = "roleCode", description = "角色编码")) @RequestParam String roleCode) {
        return Result.success(sysRoleBiz.getRoleByCode(roleCode));
    }

    /**
     * 更新角色信息
     */
    @PostMapping("/update")
    @Operation(description = "更新角色信息接口")
    public Result<Void> updateRole(@RequestBody @Valid UpdateRoleReq req) {
        sysRoleBiz.updateRole(req);
        return Result.success();
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    @Operation(description = "删除角色接口：根据ID删除角色")
    public Result<Void> deleteRole(@Parameter(schema = @Schema(name = "id", description = "角色id")) @RequestParam Long id) {
        sysRoleBiz.deleteRole(id);
        return Result.success();
    }

    /**
     * 分页查询角色信息
     */
    @PostMapping("/list")
    @Operation(description = "分页查询角色信息接口")
    public Result<BasePage<SysRoleRes>> list(@RequestBody RoleListReq req) {
        return Result.success(sysRoleBiz.list(req));
    }


    @PostMapping("/addPermission")
    @Operation(summary = "添加角色权限",description = "添加角色权限")
    public Result<Void> addPermission(@Valid @RequestBody AddPermissionReq req){
        sysRoleBiz.addPermission(req);
        return Result.success();
    }

}

